import 'package:flutter/material.dart';
import 'package:flutter_module/beans/project_bean.dart';
import 'package:flutter_module/viewModels/layer_edit_viewmodel.dart';
import 'package:flutter_module/views/fragments/project_edit_fragment.dart';
import 'package:provider/provider.dart';

///项目选择列表页面
class ProjectListFragment extends StatelessWidget {
  const ProjectListFragment({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ///路由工具
    final NavigatorState _navi = Navigator.of(context);

    ///list动画key
    final GlobalKey<AnimatedListState> _listKey =
        GlobalKey<AnimatedListState>();

    ///viewmodel
    late final LayerEditViewModel _read =
        Provider.of<LayerEditViewModel>(context, listen: false);

    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(
        ///返回按钮
        leading: BackButton(
          onPressed: () {
            _navi.pop();
          },
        ),

        ///新增项目按钮
        actions: [
          TextButton(
            onPressed: () {
              ///创建一个新项目准备编辑
              _read.setEditProject(Project(name: ""), true);

              ///跳转到新增项目页面
              _navi.push(MaterialPageRoute(builder: (_) {
                return const ProjectEditFragment();
              })).then((value) {
                ///新增项目页面新增成功后返回当前页面，listview 添加最新项
                if (value == "success") {
                  if (_read.projectList.isNotEmpty) {
                    _listKey.currentState
                        ?.insertItem(_read.projectList.length - 1);
                  }
                }
              });
            },
            child: const Text(
              "新建项目",
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
      body: AnimatedList(
        key: _listKey,
        initialItemCount: _read.projectList.length,
        itemBuilder:
            (BuildContext context, int index, Animation<double> animation) {
          return ScaleTransition(
              scale: animation,
              child: Card(
                child: Selector<LayerEditViewModel, Project>(
                  selector: (context, model) => model.projectList[index],
                  builder: (context, value, child) {
                    return ListTile(
                      title: Text(_read.projectList[index].name),
                      trailing: IconButton(
                        icon: const Icon(Icons.edit_outlined),
                        onPressed: () {
                          _read.setEditProject(_read.projectList[index], false);
                          _navi.push(MaterialPageRoute(builder: (_) {
                            return const ProjectEditFragment();
                          }));
                        },
                      ),
                      subtitle: Text(_read.projectList[index].describe),
                      onTap: () {
                        ///选择项目，返回上一页
                        _read.setCurProject(_read.projectList[index]);
                        _navi.pop();
                      },
                    );
                  },
                ),
              ));
        },
      ),
    );
  }
}
